package vn.pdg.zoombie.ui;

import java.util.ArrayList;

import vn.pdg.zoombie.bean.Image;
import vn.pdg.zoombie.bussiness.ImageLoader;
import vn.pdg.zoombie.bussiness.ManagerImages;
import vn.pdg.zoombie.common.LogDebug;
import vn.pdg.zoombie.ui.adapter.AdapterImagePicker;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class GallerryActivity extends BaseActivity implements OnItemClickListener{
	
	private String TAG = "GallerryActivity";
	private GridView gridView;
	private AdapterImagePicker adapterImagePicker;
	private ProgressDialog progressDialog;
	private ArrayList<Image> listImages;
	private ManagerImages managerImages;
	@Override
	protected void onCreate(Bundle aSavedInstanceStatea) {
		// TODO Auto-generated method stub
		super.onCreate(aSavedInstanceStatea);
		setContentView(R.layout.gallerylayout);
		loadControl();
		managerImages = new ManagerImages(this);
		progressDialog = new ProgressDialog(this);
		progressDialog.show();
		loadData();
		
	}
	public void loadControl(){
		super.loadControl();
		dataApplication.setHomeActivity(true);
		layoutMenu.setBackgroundResource(R.drawable.menu_gallery);
		gridView = (GridView) findViewById(R.id.gridViewPicker);
		gridView.setOnItemClickListener(this);
		
		titleLayout.setBackgroundResource(R.drawable.title_gallery);
	}
	
	
	
	
	private void loadData(){
		
		 
		 final Handler handler = new Handler(){
				
			 @Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				adapterImagePicker = new AdapterImagePicker(GallerryActivity.this, listImages,ImageLoader.TYPE_FROM_INTERNET);
				gridView.setAdapter(adapterImagePicker);
				progressDialog.dismiss();
			};
		 };
		 
		 new Thread(){
			 public void run(){
				 listImages = managerImages.getListImagesFromInternet();  
				
				handler.sendEmptyMessage(0); 
			 }
				 
		 }.start();
		
		
		
		
	}

 

	
	@Override
	public void onClick(View aVa) {
		// TODO Auto-generated method stub
		super.onClick(aVa);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		dataApplication.setHomeActivity(true);
		super.onResume();
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		// TODO Auto-generated method stub
		LogDebug.d(TAG, "position clicked "+ position);
		Image image 			 = listImages.get(position);
		Intent intentSelectPhoto = new Intent(GallerryActivity.this,GallerySelectedPhoto.class);
		intentSelectPhoto.putExtra(GallerySelectedPhoto.EXTRA_URL_PHOTO, image.getUrl());
		startActivity(intentSelectPhoto);
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		adapterImagePicker.getImageLoader().clearCache();
		adapterImagePicker.getImageLoader().clearCacheFile();
		super.onDestroy();
	}
	
}	

